#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int gCnt = 12345;

void test()
{
    string name("wangdao");
    int age = 100;
    int num = 1;

    //将外部所有的局部变量进行值捕获
    [=](int value){
        ++value;
        cout << "value = " << value <<endl;
        cout << "name = " << name << endl;
        cout << "age = " << age << endl;
        cout << "num = " << num << endl;
        ++gCnt;
        cout << "gCnt = " << gCnt << endl;
    }(1111);

    cout << endl <<endl;
    //将外部所有的局部变量进行引用捕获
    [&](int value){
        ++value;
        cout << "value = " << value <<endl;
        name = "wuhan";
        cout << "name = " << name << endl;
        ++age;
        cout << "age = " << age << endl;
        ++num;
        cout << "num = " << num << endl;
    }(1111);

    cout << endl <<endl;
    //name是值捕获，其他的所有外部变量是引用捕获
    [&, name](int value){
        ++value;
        cout << "value = " << value <<endl;
        cout << "name = " << name << endl;
        ++age;
        cout << "age = " << age << endl;
        ++num;
        cout << "num = " << num << endl;
    }(1111);

    cout << endl <<endl;
    //name是引用捕获，其他的所有外部变量是值捕获
    [=, &name](int value){
        ++value;
        cout << "value = " << value <<endl;
        name = "hello";
        cout << "name = " << name << endl;
        cout << "age = " << age << endl;
        cout << "num = " << num << endl;
    }(1111);

    cout << endl <<endl;
    //name是引用捕获，其他的所有外部变量是值捕获
    [age, num, &name](int value){
        ++value;
        cout << "value = " << value <<endl;
        name = "hello";
        cout << "name = " << name << endl;
        cout << "age = " << age << endl;
        cout << "num = " << num << endl;
    }(1111);
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

